package com.unicom_ng.rfid;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Build;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;

import com.unicom_ng.MainApplication;
import com.unicom_ng.R;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BeepManager implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {

    private static final String TAG = BeepManager.class.getSimpleName();

    private static final float BEEP_VOLUME = 1f;
    private static final long VIBRATE_DURATION = 200L;

    private final Context activity;
    private MediaPlayer mediaPlayer;
    private boolean playBeep;
    private Map<Integer, Integer> soundmap = new HashMap<Integer, Integer>();

    BeepManager(Context activity) {
        this.activity = activity;
        this.mediaPlayer = null;
        build();
    }

    void playBeepSoundAndVibrate(float leftVolume,float rightVolume) {
        if(soundpool!=null){
            System.out.println("开始播放声音");
            soundpool.play(soundmap.get(1),leftVolume,rightVolume,0,0,1);
            System.out.println("播放声音完成");
        }
    }

    private SoundPool soundpool;

    private void build(){
        if(Build.VERSION.SDK_INT > 21){
            SoundPool.Builder builder = new SoundPool.Builder();
            //传入音频数量
            builder.setMaxStreams(5);
            //AudioAttributes是一个封装音频各种属性的方法
            AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
            //设置音频流的合适的属性
            attrBuilder.setLegacyStreamType(AudioManager.STREAM_SYSTEM);//STREAM_MUSIC
            //加载一个AudioAttributes
            builder.setAudioAttributes(attrBuilder.build());
            soundpool = builder.build();
        }else {
            soundpool = new SoundPool(5, AudioManager.STREAM_SYSTEM, 0);
        }

        soundmap.put(1, soundpool.load(this.activity, R.raw.beep, 1));
    }
    private MediaPlayer buildMediaPlayer(Context activity) {
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnErrorListener(this);

        AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
        try {
            mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
            file.close();
            mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
            mediaPlayer.prepare();
        } catch (IOException ioe) {
            Log.w(TAG, ioe);
            mediaPlayer = null;
        }
        return mediaPlayer;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        // When the beep has finished playing, rewind to queue up another one.
        mp.seekTo(0);
    }

    @Override
    public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
        if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
            // we are finished, so put up an appropriate error toast if required and finish
//            activity.finish();
        } else {
            // possibly media player error, so release and recreate
            mp.release();
            mediaPlayer = null;
//            updatePrefs();
        }
        return true;
    }

}
